<?php
/**
 * Created by PhpStorm.
 * User: ewen
 * Date: 2020-01-02
 * Time: 11:13
 */

namespace app\admin\controller;

use app\common\model\Common;
use app\common\model\XkNormalText;
use app\common\model\XkSystemRule;
use app\common\model\XkSystemSetting;
use think\Loader;

class System extends Base
{

    /**
     * 基础设置
     */
    public function setting()
    {
        $hasAuth = checkAuth(session('adminInfo')['id'], 14);
        if (!$hasAuth) return $this->fetch('public/denied');

        if (IS_POST) {
            //  校验是否有编辑系统设置的权限
            $hasAuth = checkAuth(session('adminInfo')['id'], 13);
            !$hasAuth && backError(lang('permission denied'));

            $webName = I('post.webName/s');
            $companyName = I('post.companyName');
            $companyAddress = I('post.companyAddress');
            $serviceHotLine = I('post.serviceHotLine');
            $recordInfo = I('post.recordInfo');
            $copyrightInfo = I('post.copyrightInfo');

            $validate = Loader::validate('System');
            if (!$validate->scene('editSetting')->check(['webName' => $webName])) {
                backError($validate->getError());
            } else {
                $data = [
                    'webName' => $webName,
                    'companyName' => $companyName,
                    'companyAddress' => $companyAddress,
                    'serviceHotLine' => $serviceHotLine,
                    'recordInfo' => $recordInfo,
                    'copyrightInfo' => $copyrightInfo,
                    'updateTime' => time()
                ];
                $res = XkSystemSetting::editSetting($data);
                if ($res['status'] == 1) {
                    adminLog('后台修改网站设置，时间：' . date('Y-m-d H:i:s'));
                }
                ajaxReturn($res);
            }
        }
        return $this->fetch();
    }

    /**
     * 任务奖励
     */
    public function taskAward()
    {
        $list = XkSystemRule::order('sort desc')->select();
        foreach ($list as $k => $v) {
            if ($this->nowLang !== 'zh-cn') {
                switch ($v['id']) {
                    case 1:
                        $v['rule_des'] = 'daily sign';
                        break;
                    case 2:
                        $v['rule_des'] = 'invite friends to register and authenticate with real name';
                        break;
                    case 3:
                        $v['rule_des'] = 'options trades up to 10000 U';
                        break;
                    case 4:
                        $v['rule_des'] = 'options trades up to 50000 U';
                        break;
                    case 5:
                        $v['rule_des'] = 'options trades up to 100000 U';
                        break;
                    case 6:
                        $v['rule_des'] = 'contract trades up to 10000 U';
                        break;
                    case 7:
                        $v['rule_des'] = 'contract trades up to 50000 U';
                        break;
                    case 8:
                        $v['rule_des'] = 'contract trades up to 100000 U';
                        break;
                    case 9:
                        $v['rule_des'] = 'join partner';
                        break;
                    case 10:
                        $v['rule_des'] = 'fund trades up to 10000 U';
                        break;
                    case 11:
                        $v['rule_des'] = 'fund trades up to 50000 U';
                        break;
                    case 12:
                        $v['rule_des'] = 'fund trades up to 100000 U';
                        break;
                    case 13:
                        $v['rule_des'] = 'trading contracts or options for 7 consecutive days';
                        break;
                }
            }
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 任务奖励编辑
     */
    public function editTaskAward()
    {
        $id = I('id/d', 0);
        $info = XkSystemRule::get($id);
        if ($this->nowLang !== 'zh-cn') {
            switch ($info['id']) {
                case 1:
                    $info['rule_des'] = 'daily sign';
                    break;
                case 2:
                    $info['rule_des'] = 'invite friends to register and authenticate with real name';
                    break;
                case 3:
                    $info['rule_des'] = 'options trades up to 10000 U';
                    break;
                case 4:
                    $info['rule_des'] = 'options trades up to 50000 U';
                    break;
                case 5:
                    $info['rule_des'] = 'options trades up to 100000 U';
                    break;
                case 6:
                    $info['rule_des'] = 'contract trades up to 10000 U';
                    break;
                case 7:
                    $info['rule_des'] = 'contract trades up to 50000 U';
                    break;
                case 8:
                    $info['rule_des'] = 'contract trades up to 100000 U';
                    break;
                case 9:
                    $info['rule_des'] = 'join partner';
                    break;
                case 10:
                    $info['rule_des'] = 'fund trades up to 10000 U';
                    break;
                case 11:
                    $info['rule_des'] = 'fund trades up to 50000 U';
                    break;
                case 12:
                    $info['rule_des'] = 'fund trades up to 100000 U';
                    break;
                case 13:
                    $info['rule_des'] = 'trading contracts or options for 7 consecutive days';
                    break;
            }
        }
        $this->assign('info', $info);

        if (IS_POST) {
            $id = I('id/d', 0);
//            $rule_des = I('rule_des/s', '');
            $rule_need_num = I('rule_need_num/d', 1);
            $coin_num = I('coin_num/f', 0);
            $saveData = [
//                'rule_des' => $rule_des,
                'rule_need_num' => $rule_need_num,
                'coin_num' => $coin_num
            ];
            $res = XkSystemRule::where(['id' => $id])->save($saveData);
            backSuccess();
        }

        return $this->fetch();
    }

    /**
     * 攻略
     */
    public function strategy()
    {
        $info = XkNormalText::get(1);
        $this->assign('info', $info);

        if (IS_POST) {
            $id = I('id/d', 0);
            $content = I('content', '', 'html_entity_decode');//内容
            $enContent = I('enContent', '', 'html_entity_decode');//内容
            $res = XkNormalText::where(['id' => $id])->save([
                'content' => $content,
                'enContent' => $enContent,
                'updateTime' => time()
            ]);
            $res ? backSuccess() : backError();
        }

        return $this->fetch();
    }

    /**
     * 最高奖励
     */
    public function maxAward()
    {
        if (IS_POST) {
            $adminLogo = I('adminLogo/f', 0);
            $webLogo = I('webLogo/f', 0);

            $res = XkSystemSetting::where(['id' => 1])->save([
                'adminLogo' => $adminLogo,
                'webLogo' => $webLogo,
                'updateTime' => time()
            ]);
            $res && clearCacheByKey(Common::CACHE_SYSTEM_BASE_INFO);
            $res ? backSuccess() : backError();
        }
        return $this->fetch();
    }

}